//
// Created by LiuYou on 2021/10/28.
//

#include <iostream>
#include <cstring>


//static void push1(void* args) {
//    fputs( static_cast<char*>(args), stdout );
//}


static void* startRoutine(void* args) {

    fputs( "线程正在工作！\n", stdout );


    //<editor-fold desc="使用lambda表达式。">
    ////        pthread_cleanup_push( [](void*) {}, nullptr);
//
//        pthread_cleanup_push( push1, (void*) "push1\n" );
//            pthread_cleanup_push( push1, (void*) "push2\n" );
//                pthread_cleanup_push( push1, (void*) "push3\n" );
//
//                    fputs( "push over.\n", stdout );
//
//                pthread_cleanup_pop( 1 );
//            pthread_cleanup_pop( 0 );
//        pthread_cleanup_pop( 1 );
    //</editor-fold>

    // 使用 lambda表达式也是可以的。
    pthread_cleanup_push( [](void* args) {
        fputs( static_cast<char*>(args), stdout );
    }, (void*) "push1\n" );
        pthread_cleanup_push( [](void* args) {
            fputs( static_cast<char*>(args), stdout );
        }, (void*) "push2\n" );
            pthread_cleanup_push( [](void* args) {
                fputs( static_cast<char*>(args), stdout );
            }, (void*) "push3\n" );

                fputs( "push over.\n", stdout );

            pthread_cleanup_pop( 1 );
        pthread_cleanup_pop( 0 );
    pthread_cleanup_pop( 1 );


    pthread_exit( nullptr );
}


int main(int argc, char** argv) {

    fputs( "开始！", stdout );

    pthread_t tid{};
    int ret = pthread_create( &tid, nullptr, startRoutine, nullptr );
    if (ret != 0) {
        fprintf( stderr, "pthread_create(): %s.\n", strerror(errno));
        return 1;
    }

    pthread_join( tid, nullptr );


    fputs( "结束！", stdout );


    return 0;
}
